﻿using System;
using Brjnk.Common;
using Brjnk.GpxExplorer.UI;
using Brjnk.GpxExplorer.Workspace.Searching;

namespace Brjnk.GpxExplorer.Workspace
{
    public class Clipboard
    {
        private DataModel data;
        private SearchService searchService;
        private ClipboardWatcher watcher;

        public Clipboard(ClipboardWatcher clipboard, DataModel data, SearchService searchService)
        {
            this.data = data;
            this.searchService = searchService;
            this.watcher = clipboard;
            clipboard.Register();
            clipboard.ClipboardChanged += new ClipboardWatcher.ClipboardChangedEventHandler(clipboard_ClipboardChanged);
        }

        private void clipboard_ClipboardChanged()
        {
            var currentText = System.Windows.Clipboard.GetText();
            if (!string.IsNullOrEmpty(currentText) && currentText != lastText)
            {
                GpsCoordinates coordinates; DateTime dateTime;
                if (GpsCoordinates.TryParse(currentText, out coordinates)) ProcessCoordinates(coordinates);
                else if (DateTime.TryParse(currentText, out dateTime)) ProcessDateTime(dateTime);
            }
            lastText = currentText;
        }

        private void ProcessDateTime(DateTime dateTime)
        {
            var item = new DateTimeFromClipboard(dateTime);
            var settings = App.Instance.Settings.ClipboardSettings;
            item.Search = settings.DateTimeSearch;
            item.SearchColor = settings.DateTimeSearchColor;
            if (settings.DateTimeAsk)
                Ask(item);
            else item.Execute(data, searchService);
        }

        private void ProcessCoordinates(GpsCoordinates coordinates)
        {
            var item = new CoordinatesFromClipboard(coordinates, GetNextWaypointName());
            var settings = App.Instance.Settings.ClipboardSettings;
            item.CreateWaypoint = settings.CoordinatesCreateWaypoint;
            item.Search = settings.CoordinatesSearch;
            item.SearchColor = settings.CoordinatesSearchColor;
            if (settings.CoordinatesAsk)
                Ask(item);
            else item.Execute(data, searchService);
        }

        private ImportFromClipboardWindow askWindow;

        private void Ask(ItemFromClipboard item)
        {
            if (askWindow == null)
            {
                askWindow = new ImportFromClipboardWindow(data, searchService);
                askWindow.Closed += new EventHandler(askWindow_Closed);
                askWindow.Show();
            }
            askWindow.AddItem(item);
        }

        private void askWindow_Closed(object sender, EventArgs e)
        {
            askWindow = null;
        }

        private int waypointNumber;

        private string GetNextWaypointName()
        {
            waypointNumber++;
            return string.Format("Clipboard{0}", waypointNumber);
        }

        private string lastText;

        public void SetText(string text)
        {
            lastText = text;
            System.Windows.Clipboard.SetText(text);
        }
    }
}